package org.moka.ioio.ioiopilot.gui;

import org.moka.ioio.ioiopilot.IOIOThread;
import org.moka.ioio.ioiopilot.input.AbstractInput;
import org.moka.ioio.ioiopilot.input.PhoneSensorInputs;

import android.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.ToggleButton;

public class GraphSurface extends SurfaceView implements SurfaceHolder.Callback,
OnTouchListener {

	public static final int PEN_COLOR = Color.GRAY;
 
	private static final int GRAPH_WIDTH = 800;
	private static final int GRAPH_HEIGHT = 480;

	private static final float GRAPH_YSCALE = 0.33f;

	private GraphViewThread mThread;

	private Paint paint = new Paint();

	public boolean invalidated = true;

	private Context _context;

	private AbstractInput sensors;

	private int[][] y = new int[12][GRAPH_WIDTH/4];
	private int index = 0;

	private int[] colors = {Color.argb(255, 255, 0, 0), Color.argb(255, 0, 255, 0), Color.argb(255, 100, 100, 100), 
							Color.argb(255, 255, 127, 0), Color.argb(255, 0, 255, 200), Color.argb(255, 200, 200, 200),
							Color.argb(127, 255, 255, 0), Color.argb(127, 0, 255 , 255), Color.argb(127, 255, 255 , 255),
							Color.argb(127, 255, 0, 0), Color.argb(127, 0, 255 , 0), Color.argb(127, 100, 100 , 100)
	};

	private LinearLayout graphLegend;
 
	
	public GraphSurface(Context context) {
		super(context);
		_context = context;
		setOnTouchListener(this);
		init();
	}
 
	public GraphSurface(Context context, AttributeSet attrib) {
		super(context, attrib);
		_context = context;
		setOnTouchListener(this);
		init();
		
	}
	

	public void init()
	{
		getHolder().addCallback(this);
		mThread = new GraphViewThread(this);

		setFocusable(true);
		setFocusableInTouchMode(true);
		this.setOnTouchListener(this);

		paint.setAntiAlias(true);
		paint.setStrokeCap(Paint.Cap.ROUND);		
		
	}

	
	
public void doUpdate()
{
	if(sensors!=null && sensors.value != null) 
	{
		for(int s=0; s<y.length; s++)
		{
			y[s][index%(GRAPH_WIDTH/4)] = (int) (GRAPH_HEIGHT/2 + sensors.value[s]*GRAPH_YSCALE);
		}
		index ++;
	}
}

	public void doDraw(Canvas canvas) {
		
	
		
		

		
			paint.setStrokeWidth(2);
			canvas.drawARGB(255, 0, 0, 0);
			
			for(int s=0; s<y.length; s++) 
			{
				int rId = _context.getResources().getIdentifier("legend_"+s, "id", _context.getPackageName());
				//boolean toggle = ((ToggleButton)graphLegend.findViewById(rId)).isChecked();
				//if(toggle)
				{
					int old_y=y[s][0];
					paint.setColor(colors[s]);
					
					for(int x=y[s].length-1; x>0; x--)
					{			
						
						if((index-1)%(GRAPH_WIDTH/4)==x)
						{
							paint.setColor(Color.WHITE);
							paint.setStrokeWidth(6);
							canvas.drawLine(4*x, y[s][x], 4*x+1, y[s][x], paint);
							paint.setStrokeWidth(2);
							paint.setColor(colors[s]);
						}else
						{
							canvas.drawLine(4*x, y[s][x], 4*x+4, old_y, paint);
							
						}
						old_y = y[s][x];
					}	
				}
			}
		}
		
		
		//finishRedraw();
	

	public boolean onTouch(View view, MotionEvent event) {
		
		return true;
	}



	public synchronized void redraw() {
		invalidated = true;
	}

	public synchronized void finishRedraw() {
		invalidated = false;
	}

	public synchronized boolean shouldRedraw() {
		return invalidated;
	}

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {

	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		if (!mThread.isAlive()) {
			mThread = new GraphViewThread(this);
			mThread.setRunning(true);
			mThread.start();
		}
		Log.d(IOIOThread.TAG, "surfaceCreated()"); 
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		if (mThread.isAlive()) {
			mThread.setRunning(false);
			Log.d(IOIOThread.TAG, "surfaceDestroyed()");
		}
	}

	public void setSensors(AbstractInput sensors_) {
		
		sensors = sensors_;
	}

	public void addLegend(LinearLayout graphLegend_) {
		graphLegend = graphLegend_;
		
	}

	

	
	
	

}







